<?php
//模型
class UserPhysicalModel extends MY_Model {
	function __construct(){
		parent::__construct();
		$this->table = 'user_physical';
	}
	//分页列表
	function get_list($arr=array(),$page=1,$size=10){
		$where = " where 1 ";
		if(isset($arr['user_id']) && !empty($arr['user_id'])){
			$where .= " and  p.user_id='{$arr['user_id']}' ";		
		}		
		
		$sql  = "select p.*,t.name as type_name from ".$this->db->dbprefix.$this->table." as p left join ".$this->db->dbprefix."user_physical_type as t on p.type_id=t.id order by p.date desc LIMIT ".($page-1)*$size .",{$size}";
		$query = $this->db->query($sql);
		$res = $query->result_array();		
		
		$sql  = "select count(*) as num from ".$this->db->dbprefix.$this->table." as p  ".$where;
		$query = $this->db->query($sql);
		$row = $query->row_array();		
		return array('list'=>$res,'count' =>$row['num']);		
	}
	//全部列表
	function get_lists($arr=array()){
		$where = " where 1 ";		
		if(isset($arr['user_id']) && !empty($arr['user_id'])){
			$where .= " and  p.user_id='{$arr['user_id']}' ";		
		}
		if(isset($arr['type_id']) && !empty($arr['type_id'])){
			$where .= " and  p.type_id='{$arr['type_id']}' ";		
		}
		
		$sql  = "select p.*,t.name as type_name from ".$this->db->dbprefix.$this->table." as p left join ".$this->db->dbprefix."user_physical_type as t on p.type_id=t.id ".$where." order by p.date ";
		$query = $this->db->query($sql);
		$res = $query->result_array();
		
		return $res;		
	}
	//详情
	function get_info($id){		
		$sql  = "select * from ".$this->db->dbprefix.$this->table." where id='{$id}' ";
		$query = $this->db->query($sql);
		$row = $query->row_array();		
		return $row;
	}
	//唯一条件获取详情
	function unique_get_info($field,$value){
		$sql  = "select * from ".$this->db->dbprefix.$this->table." where {$field}='{$value}' ";
		$query = $this->db->query($sql);
		$row = $query->row_array();		
		return $row;
	}
	
	//日期类型获取详情
	function date_info($user_id,$type_id,$date){		
		$sql  = "select * from ".$this->db->dbprefix.$this->table." where user_id='{$user_id}' and type_id='{$type_id}' and `date`='{$date}' ";
		$query = $this->db->query($sql);
		$row = $query->row_array();
		return $row;
	}
	
	//身体档案
	function get_new_info($user_id=0,$page=1){
		
		if(!$user_id){
			return false;
		}
		
		$where = " user_id='{$user_id}' ";
		
		$sql  = "select date from ".$this->db->dbprefix.$this->table." where {$where} group by `date` order by date desc limit ".($page-1).",1";
		$query = $this->db->query($sql);
		$ls_row = $query->row_array();
		
		if($ls_row){
			$date = $ls_row['date'];
		}
		else{
			$date = date('Y-m-d');
		}
		
		
		$sql  = "select * from ".$this->db->dbprefix."user_physical_type  ";
		$query = $this->db->query($sql);
		$type_list = $query->result_array();
		
		$arr = array();
		$i = 0;
		foreach($type_list as $k=>$v){
			$sql  = "select record from ".$this->db->dbprefix.$this->table." where user_id='{$user_id}' and type_id='{$v['id']}' AND `date`='{$date}' limit 1;";
			$query = $this->db->query($sql);
			$row = $query->row_array();	
			
			$arr[$i]['value'] 	= $row?$row['record']:'';
			//加入类型
			$arr[$i]['type_id']   = $v['id'];
			$arr[$i]['type_name'] = $v['name'];
			$arr[$i]['code'] 	  = $v['code'];
			$arr[$i]['unit']      = $v['unit'];
			$i++;
		}
		
		$sql = "select count(*) as num from (select count(id) from ".$this->db->dbprefix.$this->table." where {$where} group by `date`) as u";
		$query = $this->db->query($sql);
		$ls_row = $query->row_array();
		$count = $ls_row['num'];
		
		return array('list'=>$arr,'count'=>$count,'date'=>$date);
	}
	
}

